package com.weilive.pay.gate;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jboss.netty.util.CharsetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.weilive.core.netty.http.HttpServlet;
import com.weilive.core.utils.XPathHelper;
import com.weilive.pay.service.weix.WxPayService;

/**
 * 微信支付通知地址
 * 
 * @author houzhiwei
 *
 */
@Service
public class WxPayServlet extends HttpServlet {
	private static final Logger logger = LoggerFactory.getLogger(WxPayServlet.class);
	@Autowired
	private WxPayService wxService;

	@Override
	public String getServletPath() {
		return "/pay/wx";
	}

	@Override
	public HttpResponse processRequest(HttpRequest request) throws Exception {
		// post
		String temStr = URLDecoder.decode(request.getContent().toString(CharsetUtil.UTF_8), "UTF-8");

		logger.info("wxResult=" + temStr);
		try {
			// 处理支付结果
			Map<String, String> params = XPathHelper.toMap(temStr);
			if (params == null || params.isEmpty()) {
				Map<String, String> toMap = new HashMap<String, String>();
				toMap.put("return_code", "SUCCESS");
				toMap.put("return_msg", "OK");

				String toXml = XPathHelper.toXmlNode(toMap);
				logger.info("收到微信支付通知, params=" + temStr + ",返回结果=" + toXml);
				return createXmlResponse(toXml);
			}

			// SUCCESS/FAIL
			// 验证并记录日志
			String check = wxService.checkAndSaveLog(params);
			// 如果ok,就直接返回
			if (check.equals("ok")) {
				Map<String, String> toMap = new HashMap<String, String>();
				toMap.put("return_code", "SUCCESS");
				toMap.put("return_msg", "OK");

				String toXml = XPathHelper.toXmlNode(toMap);
				logger.info("收到微信支付通知, params=" + params.toString() + ",返回结果=" + toXml);
				return createXmlResponse(toXml);
			}
			// 发起支付
			String result = wxService.pay(params);

			if (result.equals("ok")) {
				Map<String, String> toMap = new HashMap<String, String>();
				toMap.put("return_code", "SUCCESS");
				toMap.put("return_msg", "OK");

				String toXml = XPathHelper.toXmlNode(toMap);
				logger.info("收到微信支付通知, params=" + params.toString() + ",返回结果=" + toXml);
				return createXmlResponse(toXml);
			} else {
				Map<String, String> toMap = new HashMap<String, String>();
				toMap.put("return_code", "FAIL");
				toMap.put("return_msg", result);

				String toXml = XPathHelper.toXmlNode(toMap);
				logger.info("收到微信支付通知, params=" + params.toString() + ",返回结果=" + toXml);
				return createXmlResponse(toXml);
			}

		} catch (Exception e) {
			e.printStackTrace();

			Map<String, String> toMap = new HashMap<String, String>();
			toMap.put("return_code", "FAIL");
			toMap.put("return_msg", "server error!");

			String toXml = XPathHelper.toXmlNode(toMap);
			logger.info("收到微信支付通知, params=" + temStr + ",返回结果=" + toXml);
			return createXmlResponse(toXml);
		}
	}
}